package top.cdmrx.demo.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import top.cdmrx.common.utils.JwtUtils;
import top.cdmrx.common.utils.MD5;
import top.cdmrx.demo.ucenter.entity.Member;
import top.cdmrx.demo.ucenter.entity.vo.RegisterVo;
import top.cdmrx.demo.ucenter.mapper.MemberMapper;
import top.cdmrx.demo.ucenter.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-12-01
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String loginUser(Member member) {
        String mobile = member.getMobile();
        String password = member.getPassword();

        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new RuntimeException("登录失败,手机号或密码为空！");
        }
        //判断手机号是否正确
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Member member1 = baseMapper.selectOne(wrapper);
        //判断查询对象是否为空
        if (member1 == null){
            throw  new RuntimeException("登录失败，手机号错误！");
        }

        //判断密码是否正确
        if (!MD5.encrypt(password).equals(member1.getPassword())){
            throw  new RuntimeException("用户名或密码错误！");
        }

        //判断是否被禁用
        if (member1.getIsDisabled()){
            throw  new RuntimeException("登录失败，用户被禁用！");
        }

        //登陆成功
        String token = JwtUtils.getJwtToken(member1.getId(), member1.getNickname());
        return token;
    }

    @Override
    public void registerUser(RegisterVo registerVo) {

        String nickName = registerVo.getNickName();
        String password = registerVo.getPassword();
        String phone = registerVo.getPhone();
        String code = registerVo.getCode();

        if (StringUtils.isEmpty(nickName) || StringUtils.isEmpty(password)
            || StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)){
            throw new RuntimeException("注册失败！");
        }

        //校验验证码
        String s = redisTemplate.opsForValue().get(phone);
        if (!s.equals(code)){
            throw new RuntimeException("注册失败！");
        }

        //判断手机号是否重复注册
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",phone);
        int count= baseMapper.selectCount(wrapper);
        if (count != 0){
            throw new RuntimeException("手机号已注册！");
        }

        //判断成功之后加入到数据库
        //将注册对象中的密码加成设置，方便下面的属性传入
        Member member = new Member();
        member.setNickname(nickName);
        member.setMobile(registerVo.getPhone());
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS" +
                "4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        baseMapper.insert(member);


    }
}
